import { axios } from '../../utils/request'

// pages/start/start.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    initData: [] as Room[],
  },
  //跳转房间详情
  toDetail(e: any) {
    let item = e.currentTarget.dataset.index
    let currenTdata = this.data.initData[item]
    currenTdata.isLike = true
    wx.navigateTo({
      url: '/pages/roomDetail/roomDetail',
      success: res => {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('context', currenTdata)
      },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    wx.showLoading({ title: '加载中', mask: true })
    const isLogin = wx.getStorageSync('isLogin')
    if (isLogin) {
      this.getTabBar().setData({
        active: 1,
      })
      //获取所有收藏数据
      let result = (await axios('/start/room')) as Response
      this.setData({
        initData: result.data,
      })
    } else {
      wx.navigateTo({ url: '/pages/login/login' })
    }
    setTimeout(function () {
      wx.hideLoading()
    }, 200)
  },
})
